package me.caofeng.pojo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import me.caofeng.enums.CheckStatus;
import me.caofeng.serializer.CheckStatusSerializer;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.pojo
 * @Description: 购物车商品信息
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-25 16:17
 * @Version: V1.0
 */
public class CartItem extends Pojo{
    /**
     * 用户id
     */
    private String userId;

    /**
     * 商品编号
     */
    private int productId;

    /**
     * 商品数量
     */
    private Integer quantity;

    /**
     * 产品简要信息
     */
    private Product product;

    /**
     *
     * 是否选中
     */
    private CheckStatus checked;

    /**
     * 是否超过库存，true超过，false没有
     */
    private boolean limit;

    /**
     * 购物车商品价格
     */
    private Integer price;

    public CartItem() {
    }

    public CartItem(String userId, int productId, Integer quantity,Integer stock) {
        this.userId = userId;
        this.productId = productId;
        if ( quantity > stock){
            this.quantity = stock;
            this.limit = true;
        }else {
            this.quantity = quantity;
            this.limit = false;
        }

    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public void updateQuantity(int quantity,int stock){
        if (quantity > stock){
            this.quantity = stock;
            this.limit = true;
        }else {
            this.quantity = quantity;
            this.limit = false;
        }
    }

    @JsonSerialize(using = CheckStatusSerializer.class)
    public CheckStatus getChecked() {
        return checked;
    }

    public void setChecked(CheckStatus checked) {
        this.checked = checked;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public boolean isLimit() {
        return limit;
    }

    public void setLimit(boolean limit) {
        this.limit = limit;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
        if (null != product.getPrice()){
            this.price = this.quantity * product.getPrice();
        }
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "CartItem{" +
                "userId='" + userId + '\'' +
                ", productId=" + productId +
                ", quantity=" + quantity +
                ", checked=" + checked +
                ", id=" + id +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", isDelete=" + isDelete +
                '}';
    }
}
